package com.xy.tianyu.module.news.fragment

import androidx.lifecycle.rxLifeScope
import androidx.recyclerview.widget.DividerItemDecoration
import androidx.recyclerview.widget.LinearLayoutManager
import com.xy.tianyu.LazyLoadBaseFragment
import com.xy.tianyu.R
import com.xy.tianyu.WebActivity
import com.xy.tianyu.module.news.adapter.NewsListMultipleAdapter
import com.xy.tianyu.module.news.bean.Data
import com.xy.tianyu.module.news.bean.NewsListBean
import com.xy.tianyu.module.news.bean.NewsListMultiItem
import com.xy.tianyu.network.Api
import com.xy.tianyu.network.ApiParams
import com.xy.tianyu.utils.TimeUtils
import kotlinx.android.synthetic.main.fragment_news_child.*
import rxhttp.RxHttp
import rxhttp.toClass

/**
 * @time 2020/9/16 16:27
 * @class 描述:
 */
class NewsChildFragment :LazyLoadBaseFragment(){
    private val index = 10
    private lateinit var channel:String
    private var page  = 1
    private var oldTime: String = "0"
    private var newTime: String = "0"
    private lateinit var newListAdapter: NewsListMultipleAdapter
    private var newsList :MutableList<NewsListMultiItem> = mutableListOf()
    private var isRefresh = true

    override fun setContentView(): Int {
        return R.layout.fragment_news_child
    }


    override fun lazyLoad() {
        val channelBean = arguments?.getParcelable<Data>("news_channel")
        channelBean?.let {
            channel = channelBean.c
            refresh_news.autoRefresh()
            request()
        }

        refresh_news.setOnRefreshListener {
            oldTime = "0"
            newTime = "0"
            page = 1
            isRefresh = true
            request()
        }

        refresh_news.setOnLoadMoreListener {
            newTime = TimeUtils.getCurrentTimeStr()
            isRefresh = false
            page++
            request()
        }
        ry_news.layoutManager = LinearLayoutManager(activity,LinearLayoutManager.VERTICAL,false)
        newListAdapter = NewsListMultipleAdapter(newsList)
        ry_news.adapter = newListAdapter

        newListAdapter.setOnItemClickListener { _, _, position ->
            val url = newListAdapter.data[position].re.u
            WebActivity.actionStart(activity!!, url)
        }

    }

    override fun stopLoad() {

    }


    private fun request() {
        rxLifeScope.launch ({
            val newsListBean = getNewsList()
            newsListBean?.let {
                val reList = it.data.res
                if (reList.isNotEmpty()){
                    oldTime = TimeUtils.getCurrentTimeStr()
                    if (isRefresh) newsList.clear()
                    for (re in reList){
                        val type = re.type
                        val i = re.i
                        var newsListMultiItem:NewsListMultiItem
                        newsListMultiItem = if (type == "new_top" && isRefresh){
                            //置顶的内容
                            NewsListMultiItem(NewsListMultiItem.NEWS_TOP,re)
                        }else{
                            if (i.isNullOrEmpty()){
                                NewsListMultiItem(NewsListMultiItem.NEWS_TEXT,re)
                            }else{
                                val iList = i.split("|")
                                if (iList.size == 1){
                                    NewsListMultiItem(NewsListMultiItem.NEWS_ONE_PIC,re)
                                }else{
                                    NewsListMultiItem(NewsListMultiItem.NEWS_THREE_PIC,re)
                                }
                            }
                        }

                        newsList.add(newsListMultiItem)
                    }
                    newListAdapter.notifyDataSetChanged()
                }
            }
        },{

        },{

        },{
            refresh_news.finishRefresh()
            refresh_news.finishLoadMore()
        })
    }
    private suspend fun getNewsList():NewsListBean{
        var c :String = channel
        if (c == "local"){
            c = "local_ShenZhen_GuangDong"
        }
        return RxHttp.get(Api.THREE_HUNDRED_AND_SIXTY_NEWS_BASE_URL+Api.THREE_HUNDRED_AND_SIXTY_NEWS_LIST_URL)
            .addAll(ApiParams.getNewsListParams(newTime,oldTime,c,page.toString(),index.toString()))
            .toClass<NewsListBean>()
            .await()
    }

}